﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Threading;

namespace Dotsad.WFField
{
    public class StatusField : SPFieldUrl
    {

        public StatusField(SPFieldCollection fields, string fieldName)
            : base(fields, fieldName)
        {
            Init();
        }

        public StatusField(SPFieldCollection fields, string typeName, string displayName)
            : base(fields, typeName, displayName)
        {

            Init();
        }


        public override string DefaultValue
        {
            get
            {
                return "http://useless.site";
            }
            set
            {
                base.DefaultValue = value;
            }
        }

        private void Init()
        {
            this.DisplayFormat = SPUrlFieldFormatType.Image;
            // doesn't really make much sense to display the captured value, but feel free to whack the following lines if you want to see the entered/captured value
            this.ShowInDisplayForm = false;
            this.ShowInEditForm = false;
            this.ShowInNewForm = false;
            this.DefaultValue = "http://useless.site";
            base.DefaultValue = "http://useless.site";
        }

        public override void OnAdded(SPAddFieldOptions op)
        {
            /*We will need to update the field again after it is added to save the custom setting properties. For more
             * info see http://msdn.microsoft.com/en-us/library/cc889345(office.12).aspx#CreatingWSS3CustomFields_StoringFieldSetting */
            base.OnAdded(op);
            base.DefaultValue = "http://useless.site";
            this.Update();
        }

        public override void Update()
        {
            base.SetCustomProperty("WorkFlowProp", Thread.GetData(Thread.GetNamedDataSlot("WorkFlowProp")));
            base.SetCustomProperty("WorkFlowAjax", Thread.GetData(Thread.GetNamedDataSlot("WorkFlowAjax")));
            base.SetCustomProperty("WorkFlowStaticName", Thread.GetData(Thread.GetNamedDataSlot("WorkFlowStaticName")));
            base.Update();
        }

        public override object GetFieldValue(string value)
        {
            if (string.IsNullOrEmpty(value))
                return base.GetFieldValue(value);
            string guid = (string)base.GetCustomProperty("WorkFlowProp");
            string staticName = (string)base.GetCustomProperty("WorkFlowStaticName");
            bool render = (bool)base.GetCustomProperty("WorkFlowAjax");
            return string.Format("{0},{1},{2},{3}", value, guid, render.ToString(), staticName);
        }
    }
}
